package com.swn.cm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swn.cm.service.MenuService;
import com.swn.common.constants.ICommonConstants;
import com.swn.common.util.JSONUtil;

/**
 * 菜单管理的类
 * @author 董士明
 * @date 2018-01-11
 *
 */
//在这里面写
@Controller
@RequestMapping("/menu")
public class MenuController {
	private static Logger logger = Logger.getLogger("MenuController.class");
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * TODO 菜单权限
	 * 
	 * @param roleId
	 * @return Map<String,Object>
	 *
	 * @Author 袁文雪
	 * @Date 2018年2月7日 下午2:47:20 
	 * @Note <这里是方法的备注>
	 * 修改日期  修改人  修改内容
	 */
	
	@RequestMapping(value = "/queryMenuByRoleID",method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> queryMenuByRoleID(HttpSession session) {
		List<Map<String, Object>> map=null;
		try {
			@SuppressWarnings("unchecked")
			List<Map<String, Object>>uselist=(List<Map<String, Object>>)session.getAttribute(ICommonConstants.LOGIN_MENUROLE);
			if(uselist!=null && uselist.size()>0){
				map=uselist;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return map;
	
	}
	/**
	 * 查询菜单
	 * @return
	 */
	@RequestMapping(value = "/queryMenuList")
	@ResponseBody
	public List<Map<String,Object>> queryMenuList(){
		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = menuService.queryMenuList();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return list;
	}
	/**
	 * 查询具体功能列表
	 * @return
	 */
	@RequestMapping(value = "/queryWarrantList")
	@ResponseBody
	public List<Map<String,Object>> queryWarrantList(){
		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = menuService.queryWarrantList();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return list;
	}
	/**
	 * 添加，修改菜单
	 * @param signupForm
	 * @return
	 */
	@RequestMapping(value = "/saveMenu" , method = RequestMethod.POST)
	@ResponseBody
	public String saveMenu(String menuJsonForm){
		String result = "fail";
		try {
			Map<String,Object> paramMap=new HashMap<String,Object>();
			@SuppressWarnings("unchecked")
			Map<String,Object> menuMap = JSONUtil.getMapFromJsonString(menuJsonForm);
			boolean tag= menuService.saveMenu(menuMap);
			if(tag){result="success";}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 根据id查询菜单
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/queryMenuMapById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryMenuMapById(String id) {
		Map<String, Object> map=null;
		try {
			map = menuService.queryMenuMapById(id);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return map;
	}
	
	
	
	/**
	 * 删除菜单
	 * 
	 * @param id
	 * @return String
	 */
	@RequestMapping(value = "/deleteMenu")
	@ResponseBody
	public String deleteMenu(String id) {
		String result = "fail";
		try {
			boolean tag=menuService.querySubMenu(id);
			if (!tag){ //没有子菜单
				tag =menuService.queryBingByRole(id); //是否被角色绑定
				if(tag){
					result="hasRole";
				}else{
					 tag = menuService.deleteMenu(id);
					 result = tag?"success":"fail";
				}
				
			}else{
				result="hasSub"; 
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}

}
